<?php

namespace App\Http\Requests\Transport;

use App\Http\ValidateResult;
use Illuminate\Support\Facades\Validator;

trait OrderRequest 
{
    //预报下单
    public static function preorder_check(array $params): ValidateResult {
        //预报下单必填项：
        //选择客户、目的地口岸、货物属性、报关方式、预报重量、包装、入仓方式。

        $validator = Validator::make($params, [
//            'customer_id' => 'required|integer',
//            'destination' => 'required',
            'goods_attr' => 'required|in:1,2',
            'clearance_mode' => 'required|in:1,2,3',
            'pre_weight' => 'required|numeric',
            'packages' => 'required|array',
            'packages.*' => 'integer',
            'entry_mode' => 'required',
        ], [
            'customer_id.required' => '请选择客户',
            'customer_id.integer' => '客户ID格式不正确',
            'destination.required' => '请选择目的地',
//            'airport_id.exists' => '目的地不存在',
            'goods_attr.required' => '请选择货物属性',
            'goods_attr.in' => '货物属性不正确',
            'clearance_mode.required' => '请选择报关方式',
            'clearance_mode.in' => '报关方式不正确',
            'pre_weight.required' => '请填写预报重量',
            'pre_weight.numeric' => '预报重量格式不正确',
            'packages.required' => '请填写包装',
            'packages.array' => '包装格式不正确',
            'packages.*.integer' => '包装数量格式不正确',
            'entry_mode.required' => '请选择入仓方式',
        ]);
    
        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

//         $take_order = $params['take_order'];
//         $validator = Validator::make($take_order, [
//             'addr' => 'required',
//             'goods_name' => 'required',
//         ], [
//             'addr.required' => '请填写详细地址',
//             'goods_name.required' => '请填写品名'
//         ]);
//
//         if ($validator->fails()) {
//             return ValidateResult::error($validator->errors()->first());
//         }

         $split_order = $params['split_order'] ?? [];
         foreach ($split_order as $item) {
             $validator = Validator::make($item, [
                 'addr' => 'required',
                 'nums' => 'required',
                 'goods_name' => 'required',
                 'weight' => 'required',
             ], [
                 'addr.required' => '请填写分单详细地址',
                 'goods_name.required' => '请填写分单品名',
                 'nums' => '请填写分单件数',
                 'weight' => '请填写分单重量'
             ]);
        
             if ($validator->fails()) {
                 return ValidateResult::error($validator->errors()->first());
             }
         }

        return ValidateResult::correct();
    }

    //确认下单
    public static function confirm_check(array $params): ValidateResult {
        // 确认下单必填项：
        // 选择客户、目的地口岸、货物属性、报关方式、预报重量、包装、大包件数、入仓方式、报关清单、详细地址、品名。
        $validator = Validator::make($params, [
//            'destination' => 'required',
            'clearance_mode' => 'required|in:1,2,3',
            'entry_mode' => 'required',
            'goods_attr' => 'required|in:1,2',
            'take_order' => 'required|array',
//            'split_order' => 'array',
            "clearance_list" => 'required',
            // 'pre_weight' => 'required',
            'pre_volume' => 'required|numeric',
            'big_pkgs' => 'required|integer',
//            'small_pkgs' => 'required|integer',
        ], [
            'destination.required' => '请选择目的地',
            'clearance_mode.required' => '请选择报关方式',
            'clearance_mode.in' => '报关方式不正确',
            'entry_mode.required' => '请选择入仓方式',
            'goods_attr.required' => '请选择货物属性',
            'goods_attr.in' => '货物属性不正确',
            'take_order.required' => '请填写提单地址',
            'clearance_list.required' => '请上传报关清单',
            // 'pre_weight.required' => '请填写预报重量',
            'pre_volume.required' => '请填写预报体积',
            'big_pkgs.required' => '请填写大包件数',
            'small_pkgs.required' => '请填写小包件数'
        ]);
    
        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

        $take_order = $params['take_order'];
        $validator = Validator::make($take_order, [
            'addr' => 'required',
            'goods_name' => 'required',
        ], [
            'addr.required' => '请填写详细地址',
            'goods_name.required' => '请填写品名'
        ]);
    
        if ($validator->fails()) {
            return ValidateResult::error($validator->errors()->first());
        }

        $split_order = $params['split_order'] ?? [];
        foreach ($split_order as $item) {
            $validator = Validator::make($item, [
                'addr' => 'required',
                'nums' => 'required',
                'goods_name' => 'required',
                'weight' => 'required',
            ], [
                'addr.required' => '请填写分单详细地址',
                'goods_name.required' => '请填写分单品名',
                'nums' => '请填写分单件数',
                'weight' => '请填写分单重量'
            ]);
        
            if ($validator->fails()) {
                return ValidateResult::error($validator->errors()->first());
            }
        }

        return ValidateResult::correct();
    }

}
